// API接口配置
const request = require('../utils/request.js')

class API {
  constructor() {
    this.request = request
  }

  // ==================== 用户相关接口 ====================
  
  // 微信登录
  wechatLogin(code, userInfo) {
    return this.request.post('/users/wechat_login/', {
      code,
      user_info: userInfo
    })
  }

  // 获取用户信息
  getUserProfile() {
    return this.request.get('/users/profile/')
  }

  // 更新用户信息
  updateUserProfile(data) {
    return this.request.put('/users/update_profile/', data)
  }

  // 获取用户统计信息
  getUserStatistics() {
    return this.request.get('/users/statistics/')
  }

  // ==================== 鲜花相关接口 ====================
  
  // 获取鲜花列表
  getFlowers(params = {}) {
    return this.request.get('/flowers/', params)
  }

  // 获取鲜花详情
  getFlowerDetail(id) {
    return this.request.get(`/flowers/${id}/`)
  }

  // 获取鲜花分类
  getFlowerCategories() {
    return this.request.get('/flowers/categories/')
  }

  // 获取推荐鲜花
  getFeaturedFlowers(limit = 10) {
    return this.request.get('/flowers/featured/', { limit })
  }

  // 获取热销鲜花
  getHotFlowers(limit = 10) {
    return this.request.get('/flowers/hot/', { limit })
  }

  // 搜索鲜花
  searchFlowers(keyword, params = {}) {
    return this.request.get('/flowers/', {
      search: keyword,
      ...params
    })
  }

  // 获取轮播图
  getBanners() {
    return this.request.get('/flowers/banners/')
  }

  // ==================== 购物车相关接口 ====================
  
  // 获取购物车
  getCart() {
    return this.request.get('/orders/cart/')
  }

  // 添加到购物车
  addToCart(flowerId, quantity = 1, specifications = {}) {
    return this.request.post('/orders/cart/', {
      flower_id: flowerId,
      quantity,
      specifications
    })
  }

  // 更新购物车商品
  updateCartItem(itemId, data) {
    return this.request.put(`/orders/cart/${itemId}/`, data)
  }

  // 删除购物车商品
  removeCartItem(itemId) {
    return this.request.delete(`/orders/cart/${itemId}/`)
  }

  // 清空购物车
  clearCart() {
    return this.request.delete('/orders/cart/clear/')
  }

  // 批量更新购物车
  batchUpdateCart(items) {
    return this.request.post('/orders/cart/batch_update/', {
      items
    })
  }

  // ==================== 订单相关接口 ====================
  
  // 获取订单列表
  getOrders(params = {}) {
    return this.request.get('/orders/', params)
  }

  // 获取订单详情
  getOrderDetail(id) {
    return this.request.get(`/orders/${id}/`)
  }

  // 创建订单
  createOrder(data) {
    return this.request.post('/orders/', data)
  }

  // 取消订单
  cancelOrder(id, reason = '') {
    return this.request.post(`/orders/${id}/cancel/`, {
      reason
    })
  }

  // 确认收货
  confirmOrder(id) {
    return this.request.post(`/orders/${id}/confirm/`)
  }

  // 申请退款
  refundOrder(id, reason = '') {
    return this.request.post(`/orders/${id}/refund/`, {
      reason
    })
  }

  // 获取订单统计
  getOrderStatistics() {
    return this.request.get('/orders/statistics/')
  }

  // 订单预览（结算前）
  previewOrder(data) {
    return this.request.post('/orders/preview/', data)
  }

  // ==================== 支付相关接口 ====================
  
  // 创建支付订单
  createPayment(orderId, paymentMethod = 'WECHAT') {
    return this.request.post('/orders/payment/', {
      order_id: orderId,
      payment_method: paymentMethod
    })
  }

  // 查询支付状态
  getPaymentStatus(orderId) {
    return this.request.get(`/orders/payment/status/${orderId}/`)
  }

  // ==================== 收货地址相关接口 ====================
  
  // 获取收货地址列表
  getAddresses() {
    return this.request.get('/users/addresses/')
  }

  // 获取收货地址详情
  getAddressDetail(id) {
    return this.request.get(`/users/addresses/${id}/`)
  }

  // 添加收货地址
  addAddress(data) {
    return this.request.post('/users/addresses/', data)
  }

  // 更新收货地址
  updateAddress(id, data) {
    return this.request.put(`/users/addresses/${id}/`, data)
  }

  // 删除收货地址
  deleteAddress(id) {
    return this.request.delete(`/users/addresses/${id}/`)
  }

  // 设置默认地址
  setDefaultAddress(id) {
    return this.request.post(`/users/addresses/${id}/set_default/`)
  }

  // ==================== 优惠券相关接口 ====================
  
  // 获取优惠券列表
  getCoupons(status = 'available') {
    return this.request.get('/users/coupons/', { status })
  }

  // 领取优惠券
  claimCoupon(couponId) {
    return this.request.post(`/users/coupons/${couponId}/claim/`)
  }

  // 获取可用优惠券
  getAvailableCoupons(totalAmount) {
    return this.request.get('/users/coupons/available/', {
      total_amount: totalAmount
    })
  }

  // ==================== 收藏相关接口 ====================
  
  // 获取收藏列表
  getFavorites() {
    return this.request.get('/users/favorites/')
  }

  // 添加收藏
  addFavorite(flowerId) {
    return this.request.post('/users/favorites/', {
      flower_id: flowerId
    })
  }

  // 取消收藏
  removeFavorite(flowerId) {
    return this.request.delete(`/users/favorites/${flowerId}/`)
  }

  // 检查是否已收藏
  checkFavorite(flowerId) {
    return this.request.get(`/users/favorites/check/${flowerId}/`)
  }

  // ==================== 评价相关接口 ====================
  
  // 获取商品评价
  getReviews(flowerId, params = {}) {
    return this.request.get(`/flowers/${flowerId}/reviews/`, params)
  }

  // 添加评价
  addReview(orderId, flowerId, data) {
    return this.request.post('/orders/reviews/', {
      order_id: orderId,
      flower_id: flowerId,
      ...data
    })
  }

  // 获取我的评价
  getMyReviews(params = {}) {
    return this.request.get('/users/reviews/', params)
  }

  // ==================== 文件上传接口 ====================
  
  // 上传图片
  uploadImage(filePath, formData = {}) {
    return this.request.uploadFile({
      url: '/upload/image/',
      filePath,
      name: 'image',
      formData
    })
  }

  // 上传头像
  uploadAvatar(filePath) {
    return this.request.uploadFile({
      url: '/upload/avatar/',
      filePath,
      name: 'avatar'
    })
  }

  // ==================== 系统相关接口 ====================
  
  // 获取系统配置
  getSystemConfig() {
    return this.request.get('/system/config/')
  }

  // 获取版本信息
  getVersionInfo() {
    return this.request.get('/system/version/')
  }

  // 意见反馈
  submitFeedback(data) {
    return this.request.post('/system/feedback/', data)
  }

  // 获取公告列表
  getNotices(params = {}) {
    return this.request.get('/system/notices/', params)
  }

  // 获取帮助文档
  getHelpDocs(params = {}) {
    return this.request.get('/system/help/', params)
  }

  // ==================== 统计相关接口 ====================
  
  // 商品浏览统计
  trackView(flowerId) {
    return this.request.post('/statistics/view/', {
      flower_id: flowerId
    }, { showError: false })
  }

  // 搜索统计
  trackSearch(keyword) {
    return this.request.post('/statistics/search/', {
      keyword
    }, { showError: false })
  }

  // 分享统计
  trackShare(type, targetId) {
    return this.request.post('/statistics/share/', {
      type,
      target_id: targetId
    }, { showError: false })
  }
}

// 创建API实例
const api = new API()

module.exports = api